home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NetNews Offline 2
/
NetNews Offline Volume 2.iso
/
news
/
comp
/
std
/
c
/
567
< prev
next >
Wrap
Internet Message Format
|
1996-08-06
|
2KB
Path: mail2news.demon.co.uk!pires.demon.co.uk
From: Martin Bonner <mbonner@pires.co.uk>
Newsgroups: comp.std.c
Subject: Re: alignment requirements for all structures?
Date: Thu, 14 Mar 1996 12:41:28 GMT
Organization: Pi Technology Ltd
Distribution: world
Message-ID: <814065992wnr@pires.co.uk>
References: <1996Mar7.151216.1793@ittpub>
Reply-To: mbonner@pires.co.uk
X-NNTP-Posting-Host: pires.demon.co.uk
X-Broken-Date: Thursday, Mar 14, 1996 12.41.28 GMT
X-Newsreader: Newswin Alpha 0.6
X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!pires.demon.co.uk
In article: <1996Mar7.151216.1793@ittpub> wil@ittpub.nl (Wil Evers) writes:
> (1). Is there a rule in the C standard stating that the address of *every*
> structure - irrespective of its contents - should satisfy some alignment
> requirement? For example, is the following guaranteed to work?
>
> struct a { char data[sizeof(double)]; } ana;
> struct b { double data; };
>
> int main()
> {
> ((struct b *) &ana)->data = 42.42;
> return 0;
> }
I am fairly sure not.
> (2). If the answer to (1) is no, then is a conforming compiler allowed to
> assign some specific alignment requirement to a data type, just because it
> is defined as a structure, and again irrespective of the contents of the
> structure? For example, if I write:
>
> #include <stdio.h>
>
> struct c { char data; } carray[10];
>
> int main()
> {
> printf("%u\n", sizeof carray);
> return 0;
> }
>
> would it be legal for this program to print something other than 10?
Almost certainly YES
> (3). If the answer to (2) is yes, why would a compiler want to do this?
For example:
Because the architecture is word addressed (not byte addressed), so
indexing the array is painful unless the structures are padded up to
machine words. (I can think of other reasons too).
In general, ISO C does not specify ANYTHING about additional padding that a
compiler may introduce
Martin Bonner
Pi Technology, Milton Hall, Cambridge, ENGLAND
+44 1223 441256
mbonner@pires.co.uk